package com.wanxi.controller;

import com.wanxi.model.NewsModel;
import com.wanxi.model.ResultModel;
import com.wanxi.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 杨金乾
 */
@Controller
@CrossOrigin(origins = "*")
public class NewController{
    @Autowired
    private NewsService newsService;
    @Autowired
    SendMessageController sendMessageController;
    private String table="新闻";
    @RequestMapping("/back/news/add")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')")
    protected ResultModel newsAdd(NewsModel model){

        ResultModel resultModel=newsService.add(model);
        sendMessageController.addUserMessages(table);
        //6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/news/findAll")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel newsFindAll(NewsModel model){

        ResultModel resultModel=newsService.findAll(model);
        sendMessageController.findAllUserMessages(table);
        //6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/news/findById")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel newsFindById(NewsModel model){

        ResultModel resultModel=newsService.findById(model);
        sendMessageController.findByIdUserMessages(table);
        //6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/news/delete")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')")
    protected ResultModel newsDelete(NewsModel model){

        ResultModel resultModel=newsService.delete(model);
        sendMessageController.deleteUserMessages(table);
        //6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/news/enable")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel newsUpdateEnable(NewsModel model){

        ResultModel resultModel=newsService.enable(model);
        sendMessageController.updateEnableMessages(table);
        //6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/news/isRecommend")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel newsUpdateIsRecommend(NewsModel model){

        ResultModel resultModel=newsService.isRecommend(model);
        sendMessageController.updateIsRecommendMessages(table);
        //6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/news/isShow")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel newsIsShow(NewsModel model){

        ResultModel resultModel=newsService.isShow(model);
        sendMessageController.updateIsShowMessages(table);
        //6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/news/update")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')")
    protected ResultModel newsUpdate(NewsModel model){

        ResultModel resultModel=newsService.update(model);
        sendMessageController.updateMessages(table);
        //6.返回前端
        return resultModel;
    }
}
